﻿using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;

namespace Corina.Controls.Silverlight.Behaviours
{
    public class MultiSelectorBehaviours
    {
        #region Old
        /*
        private readonly ListBox _listBox;
        private readonly IList _boundList;

        public MultiSelectorBehaviours(ListBox listBox, IList boundList)
        {
            _boundList = boundList;
            _listBox = listBox;
            _listBox.SelectionChanged += OnSelectionChanged;
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _boundList.Clear();

            foreach (var item in _listBox.SelectedItems)
            {
                _boundList.Add(item);
            }
        }
        //*/
        #endregion

        #region New
        private readonly ListBox _listBox;
        private IList _boundList;

        public MultiSelectorBehaviours(ListBox listBox, IList boundList)
        {
            _boundList = boundList;
            _listBox = listBox;
            _listBox.Loaded += new RoutedEventHandler(_listBox_Loaded);
            AddCollectionChangedHandler();

        }

        void SelectedItemsBehavior_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RemoveSelectionChangedHandler();
            _listBox.SelectedItems.Clear();
            foreach (var item in _boundList)
            {
                _listBox.SelectedItems.Add(item);
            }
            AddSelectionChangedHandler();
        }

        void _listBox_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in _boundList)
            {
                _listBox.SelectedItems.Add(item);
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RemoveCollectionChangedHandler();
            _boundList.Clear();

            foreach (var item in _listBox.SelectedItems)
            {
                _boundList.Add(item);
            }
            AddCollectionChangedHandler();
        }

        public void SetNewList(IList boundList)
        {
            RemoveCollectionChangedHandler();
            _boundList = boundList;
            AddCollectionChangedHandler();
        }

        public void AddSelectionChangedHandler()
        {
            _listBox.SelectionChanged += OnSelectionChanged;
        }

        public void RemoveSelectionChangedHandler()
        {
            _listBox.SelectionChanged -= OnSelectionChanged;
        }

        public void AddCollectionChangedHandler()
        {
            if (_boundList == null) return;
            (_boundList as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedItemsBehavior_CollectionChanged);
        }

        public void RemoveCollectionChangedHandler()
        {
            if (_boundList == null) return;
            (_boundList as INotifyCollectionChanged).CollectionChanged -= new NotifyCollectionChangedEventHandler(SelectedItemsBehavior_CollectionChanged);
        }
        #endregion
    }
}
